import {createSql} from "./index";
import type {Manage} from "../types";

const tableName = "t_manage";

export const queryList = (type: number) =>
  createSql<Manage>('SELECT * FROM ' + tableName + ' WHERE TYPE = ? ORDER BY `rank` ASC', [type]);

export async function getRank(){
  const manageList = await createSql<Manage[]>('SELECT * FROM ' + tableName + ' ORDER BY `rank` DESC LIMIT 1')
  return manageList.length > 0 ? manageList[0].rank + 1 : 1;
}

export const insert = async (data: Manage) =>{
  return  createSql<Manage>(
      'INSERT INTO '+ tableName + ' (id, name, color , source, type,createTime,`rank`) VALUES ( ?,?,?,?,?,?,?)',
      [data.id, data.name, data.color, data.source, data.type,data.createTime,data.rank]
  );
}
export const deleteById = (id: string) =>{
 return createSql<Manage>(' DELETE FROM '+ tableName + ' WHERE id = ?', [id]);
}

export const update = async (data: Manage) =>{
  return  createSql<Manage>(
      'UPDATE '+ tableName + ' SET name = ?, color = ? WHERE id = ?',
      [data.name, data.color, data.id]
  );
}

export default {
  queryList,
  insert,
  deleteById,
  update,
  getRank,
  queryById: (id: number) =>
    createSql<Manage>('SELECT * FROM '+ tableName +' WHERE id =?',[id]),
}
